package cn.anc.aonicardv.module.ui.my;

import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import cn.anc.aonicardv.Configs;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.NetUtils;
import cn.anc.aonicardv.util.WiFiUtil;

/**
 * Created by yangdai on 2017/6/22.
 */

public class WebSiteActivity extends BaseActivity implements View.OnClickListener {

    private TextView titleTv;
    private WebView helpWv;
    private ProgressBar progressClb;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_website);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initVariable() {
        findViewById(R.id.tv_back).setOnClickListener(this);
        titleTv = (TextView) findViewById(R.id.tv_title);
        helpWv = (WebView) findViewById(R.id.wv_help);
        progressClb = (ProgressBar) findViewById(R.id.pb_progress);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (NetUtils.isConnectedDevices(this)) {

            WiFiUtil.getInstance(this).closeWifi();
        }
        titleTv.setText(getString(R.string.about_website));
        WebSettings webSettings = helpWv.getSettings();


        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setSavePassword(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        helpWv.setWebViewClient(webViewClient);
        helpWv.setWebChromeClient(webChromeClient);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                helpWv.loadUrl(Configs.DEFUTL_WEBSITE);

            }
        }, 500);


    }


    public WebViewClient webViewClient = new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    };

    WebChromeClient webChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (progressClb == null)
                return;
            if (newProgress == 100) {
                progressClb.setVisibility(View.GONE);
            } else {
                progressClb.setProgress(newProgress);
            }
        }

    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                break;
        }


    }
}
